<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
    <el-form label-position="right" :model="facilityMaintenanceRecordForm" :label-width="formLabelWidth" ref="refFacilityMaintenanceRecordForm" :rules="rules">
      <el-row>
        <el-col :span="11">
          <el-form-item label="公共设施名称" prop="facilityName">
            <el-input v-model="facilityMaintenanceRecordForm.facilityName" placeholder="请选择公共设施名称" readonly="readonly">
              <i slot="suffix" class="el-input__icon el-icon-search" @click="findFacility"></i>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="保养开始时间" prop="maintenanceStartDate">
            <el-date-picker
                v-model="facilityMaintenanceRecordForm.maintenanceStartDate" type="date" placeholder="保养开始时间" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="保养开始时间" prop="maintenanceEndDate">
            <el-date-picker
                v-model="facilityMaintenanceRecordForm.maintenanceEndDate" type="date" placeholder="保养开始时间" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="维修保养人员" prop="staffName">
            <el-input v-model="facilityMaintenanceRecordForm.staffName" placeholder="请选择维修保养人员" readonly="readonly">
              <i slot="suffix" class="el-input__icon el-icon-search" @click="findStaff"></i>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="facilityMaintenanceRecordForm.remark" placeholder="请输入备注" maxlength="255"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="facilitySelectorVisible" v-if="facilitySelectorVisible" title="公共设施信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <facility-selector @facility-selected-event="handleFacilitySelectedEvent">
      </facility-selector>
    </el-dialog>
    <el-dialog :visible.sync="staffSelectorVisible" v-if="staffSelectorVisible" title="维修保养人员选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <staff-selector @staff-selected-event="handleStaffSelectedEvent">
      </staff-selector>
    </el-dialog>
  </div>
</template>

<script>
import facilityMaintenanceRecordApi from '@/api/facilityMaintenanceRecordApi'
import FacilitySelector from "@/views/facility/FacilitySelector.vue";
import StaffSelector from "@/views/staff/StaffSelector.vue";
export default {
  name: 'FacilityMaintenanceRecordAdd',
  components: {FacilitySelector,StaffSelector,},
  props: ['id'],
  mixins: [
  ],
  data () {
    return {
      facilityMaintenanceRecordForm: {
        facilityId: '',
        facilityName: '',
        maintenanceStartDate: '',
        maintenanceEndDate: '',
        staffId: '',
        staffName: '',
        remark: ''
      },
      rules: {
        facilityName: [
          { required: true, message: '请输入公共设施名称', trigger: 'blur' }
        ],
      },
      facilitySelectorVisible: false,
      staffSelectorVisible: false,
    }
  },
  created () {
    if (this.id) {
      facilityMaintenanceRecordApi.findById(Number(this.id))
        .then(resp => {
          if (resp && resp.code === 1) {
            this.facilityMaintenanceRecordForm = resp.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
        })
    }
  },
  methods: {
    onReset () {
      this.facilityMaintenanceRecordForm = {
        facilityId: '',
        facilityName: '',
        maintenanceStartDate: '',
        maintenanceEndDate: '',
        staffId: '',
        staffName: '',
        remark: ''
      }
      this.$emit('reset-facility-maintenance-record-add-event')
    },
    onSave () {
      this.$refs.refFacilityMaintenanceRecordForm.validate((valid) => {
        if (valid) {
          facilityMaintenanceRecordApi.save(this.facilityMaintenanceRecordForm)
            .then((resp) => {
              if (resp && resp.code === 1) {
                this.$message.success('保存成功')
                this.$emit('close-facility-maintenance-record-add-event', {search: true})
              } else {
                this.$message.error('保存失败:' + (resp.msg ? resp.msg : ''))
              }
            })
            .catch(error => {
              console.log(error)
              this.$message.error('保存失败')
            })
        } else {
          return false
        }
      })
    },
    findFacility () {
      this.facilitySelectorVisible = true
    },
    handleFacilitySelectedEvent (selectedFacility) {
      this.facilitySelectorVisible = false
      if (selectedFacility && 'facilityId' in selectedFacility && this.facilityMaintenanceRecordForm.facilityId !== selectedFacility['facilityId']) {
        this.facilityMaintenanceRecordForm.facilityId = selectedFacility['facilityId']
        this.facilityMaintenanceRecordForm.facilityName = selectedFacility['facilityName']
      }
    },
    findStaff () {
      this.staffSelectorVisible = true
    },
    handleStaffSelectedEvent (selectedStaff) {
      this.staffSelectorVisible = false
      if (selectedStaff && 'staffId' in selectedStaff && this.facilityMaintenanceRecordForm.staffId !== selectedStaff['staffId']) {
        this.facilityMaintenanceRecordForm.staffId = selectedStaff['staffId']
        this.facilityMaintenanceRecordForm.staffName = selectedStaff['staffName']
      }
    },
    onBack () {
      this.$emit('close-facility-maintenance-record-add-event')
    }
  }
}
</script>

<style lang="scss">
</style>